home *** CD-ROM | disk | FTP | other *** search
/ Hot Super Models / Hot Super Models.iso / unix / x11 / xv200.tar / xv-2.00 / unsupt / vis < prev   
Text File  |  1992-01-01  |  48KB  |  1,591 lines

  1. (Message /usr/users/bradley/Mail/inbox:28)
  2. Return-Path: tmb@ai.mit.edu
  3. Received-Date: Wed, 13 Nov 91 07:27:59 EST
  4. Received: from life.ai.mit.edu by central.cis.upenn.edu
  5.     id AA18349; Wed, 13 Nov 91 07:27:57 -0500
  6. Posted-Date: Wed, 13 Nov 91 07:24:28 EST
  7. Received: from bambleweenie57 (bambleweenie57.ai.mit.edu) by life.ai.mit.edu (4.1/AI-4.10) id AA24831; Wed, 13 Nov 91 07:27:53 EST
  8. From: tmb@ai.mit.edu (Thomas M. Breuel)
  9. Received: by bambleweenie57 (4.1/AI-4.10) id AA05108; Wed, 13 Nov 91 07:24:28 EST
  10. Date: Wed, 13 Nov 91 07:24:28 EST
  11. Message-Id: <9111131224.AA05108@bambleweenie57>
  12. To: bradley@central.cis.upenn.edu
  13. Subject: simple VIS format reader for "xv"
  14.  
  15. Below you find a "shar" file containing code for reading the two most
  16. common types of VIS format images: 8bit unsigned and machine-dependent
  17. single precision floating point. Also included are patches necessary
  18. to add this reading function to "xv".
  19.  
  20. VIS format is a simple, flexible image format that has originated here
  21. at the AI lab and that is used by several vision research labs for
  22. storing and displaying images. It consists of header lines of the form
  23. "name=value" followed by a line containing a form-feed as the first
  24. character, followed by the raw image data.  The lines "DIMS=" and
  25. "ETYPE=" specify the dimensions of the image and the type of the
  26. binary data. Later header lines override earlier header lines, which
  27. makes it possible to keep a history in the header.
  28.  
  29. I'd appreciate if you could include this in the next version of "xv".
  30. Eventually, I may send you a nicer version, but this version seems to
  31. be working.
  32.  
  33.                     Thanks, Thomas.
  34.  
  35. # This is a shell archive.  Remove anything before this line,
  36. # then unpack it by saving it in a file and typing "sh file".
  37. #
  38. # Wrapped by volterra!tmb on Wed Nov 13 06:59:43 EST 1991
  39. # Contents:  xvvis.c xvvis.diffs
  40.  
  41. echo x - xvvis.c
  42. sed 's/^@//' > "xvvis.c" <<'@//E*O*F xvvis.c//'
  43. /*
  44.  * xvvis.c - load routine for 'vis' format pictures
  45.  *
  46.  * LoadVIS(fname, numcols)  -  loads a VIS pic, does 24to8 code if nec.
  47.  * WriteVIS(fp, pic, w, h, r,g,b, numcols, style)
  48.  * WriteRaw(fp, pic, w, h, r,g,b, numcols, style)
  49.  */
  50.  
  51. #include "xv.h"
  52.  
  53. static int VISError();
  54.  
  55. int LoadVIS(fname,nc)
  56. char *fname;
  57. int   nc;
  58. {
  59.     FILE  *fp;
  60.     char buf[1000];
  61.     int w=-1,h=-1;
  62.     char etype[100];
  63.     int raster=getenv("VIS_RASTER")&&atoi(getenv("VIS_RASTER"));
  64.     
  65.     fp=fopen(fname,"r");
  66.     if (!fp) return VISError("unable to open file");
  67.  
  68.     while(fgets(buf,sizeof buf,fp)) {
  69.         if(!buf[0]||buf[0]=='\n'||buf[0]=='\f') break;
  70.         sscanf(buf,"DIMS=%d %d",&w,&h);
  71.         sscanf(buf,"ETYPE=%s",etype);
  72.     }
  73.  
  74.     if(w<0||h<0) {fclose(fp); return VISError("bad format");}
  75.  
  76.     pWIDE=w; pHIGH=h;
  77.     {int i; for(i=0;i<256;i++) r[i]=g[i]=b[i]=i;}
  78.  
  79.     if(!strcmp(etype,"uchar")) {
  80.         int i,j;
  81.         pic=(unsigned char*)malloc(w*h);
  82.         if(!pic) {fclose(fp); return VISError("cannot allocate enough memory");}
  83.         if(!raster) for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+(h-j-1)*w]=getc(fp);
  84.         else for(j=0;j<h;j++) for(i=0;i<w;i++) pic[i+j*w]=getc(fp);
  85.     } else if(!strcmp(etype,"float")) {
  86.         int i,j,n;
  87.         float min=1e38,max=-1e38;
  88.         float *t;
  89.         t=(float*)malloc(w*h*sizeof *t);
  90.         if(!t) {fclose(fp); return VISError("cannot allocate enough memory");}
  91.         pic=(unsigned char*)malloc(w*h); 
  92.         if(!pic) {free(t); fclose(fp); return VISError("cannot allocate enough memory");}
  93.         fread(t,sizeof *t,w*h,fp);
  94.         n=w*h;
  95.         for(i=0;i<n;i++) {if(t[i]<min) min=t[i]; if(t[i]>max) max=t[i];}
  96.         if(max==min) max=min+1.0;
  97.         if(!raster) for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+(h-j-1)*w]=255*(t[i*h+j]-min)/(max-min);
  98.         else for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+j*w]=255*(t[i+j*w]-min)/(max-min);
  99.         free(t);
  100.     } else {close(fp); return VISError("unknown ETYPE");}
  101.  
  102.     fclose(fp);
  103.     return 0;
  104. }
  105.  
  106. static int VISError(st)
  107. char *st;
  108. {
  109.     SetISTR(ISTR_WARNING,"LoadVIS() - %s",cmd,st);
  110.     Warning();
  111.     return -1;
  112. }
  113. @//E*O*F xvvis.c//
  114. chmod u=rw,g=r,o=r xvvis.c
  115.  
  116. echo x - xvvis.diffs
  117. sed 's/^@//' > "xvvis.diffs" <<'@//E*O*F xvvis.diffs//'
  118. diff -c xv.orig/Imakefile xv/Imakefile
  119. *** xv.orig/Imakefile    Wed Nov 13 05:55:55 1991
  120. --- xv/Imakefile    Wed Nov 13 06:23:48 1991
  121. ***************
  122. *** 108,118 ****
  123.   
  124.   SRCS1 =    xv.c xv24to8.c xvbutt.c xvctrl.c xvdir.c xvfish.c xvgam.c\
  125.       xvgif.c xvgifwr.c xvinfo.c xvmisc.c xvpbm.c xvpm.c xvscrl.c\
  126. !     xvxbm.c vprintf.c xvjpeg.c
  127.   
  128.   OBJS1 =    xv.o xv24to8.o xvbutt.o xvctrl.o xvdir.o xvfish.o xvgam.o\
  129.       xvgif.o xvgifwr.o xvinfo.o xvmisc.o xvpbm.o xvpm.o xvscrl.o \
  130. !     xvxbm.o vprintf.o xvjpeg.o $(JOBJS)
  131.   
  132.   SRCS2=    bggen.c
  133.   OBJS2=    bggen.o
  134. --- 108,118 ----
  135.   
  136.   SRCS1 =    xv.c xv24to8.c xvbutt.c xvctrl.c xvdir.c xvfish.c xvgam.c\
  137.       xvgif.c xvgifwr.c xvinfo.c xvmisc.c xvpbm.c xvpm.c xvscrl.c\
  138. !     xvxbm.c vprintf.c xvjpeg.c xvvis.c
  139.   
  140.   OBJS1 =    xv.o xv24to8.o xvbutt.o xvctrl.o xvdir.o xvfish.o xvgam.o\
  141.       xvgif.o xvgifwr.o xvinfo.o xvmisc.o xvpbm.o xvpm.o xvscrl.o \
  142. !     xvxbm.o vprintf.o xvjpeg.o $(JOBJS) xvvis.o
  143.   
  144.   SRCS2=    bggen.c
  145.   OBJS2=    bggen.o
  146. diff -c xv.orig/xv.c xv/xv.c
  147. *** xv.orig/xv.c    Wed Nov 13 05:56:12 1991
  148. --- xv/xv.c    Wed Nov 13 06:22:39 1991
  149. ***************
  150. *** 54,59 ****
  151. --- 54,60 ----
  152.   #define PBM     3
  153.   #define XBM     4
  154.   #define JFIF    5
  155. + #define VIS 6
  156.   
  157.   static unsigned long rootbg, rootfg;  /* fg/bg for root border */
  158.   static int    waitsec = -1;     /* seconds between pics. -1=wait for event */
  159. ***************
  160. *** 784,789 ****
  161. --- 785,792 ----
  162.   
  163.     else if (strncmp(magicno,"#define",7)==0) filetype = XBM;
  164.   
  165. +   else if (strncmp(magicno,"VISF",4)==0) filetype = VIS;
  166.     if (filetype == UNKNOWN) {
  167.       SetISTR(ISTR_INFO,"'%s' not in a recognized format.", basename);
  168.       Warning();
  169. ***************
  170. *** 798,803 ****
  171. --- 801,807 ----
  172.     case PBM:  i = LoadPBM (filename,ncols); break;
  173.     case XBM:  i = LoadXBM (filename,ncols); break;
  174.     case JFIF: i = LoadJFIF(filename,ncols); break;
  175. +   case VIS: i= LoadVIS(filename,ncols); break;
  176.     }
  177.     WaitCursor();
  178.   
  179. @//E*O*F xvvis.diffs//
  180. chmod u=rw,g=rw,o=r xvvis.diffs
  181.  
  182. exit 0
  183.  
  184. Return-Path: tmb@ai.mit.edu
  185. Received-Date: Fri, 15 Nov 91 15:40:42 EST
  186. Received: from life.ai.mit.edu by central.cis.upenn.edu
  187.     id AA29972; Fri, 15 Nov 91 15:40:34 -0500
  188. Posted-Date: Fri, 15 Nov 91 15:36:41 EST
  189. Received: from bambleweenie57 (bambleweenie57.ai.mit.edu) by life.ai.mit.edu (4.1/AI-4.10) id AA21156; Fri, 15 Nov 91 15:40:10 EST
  190. From: tmb@ai.mit.edu (Thomas M. Breuel)
  191. Received: by bambleweenie57 (4.1/AI-4.10) id AA09661; Fri, 15 Nov 91 15:36:41 EST
  192. Date: Fri, 15 Nov 91 15:36:41 EST
  193. Message-Id: <9111152036.AA09661@bambleweenie57>
  194. To: bradley@central.cis.upenn.edu
  195. Subject: VIS support for xv
  196.  
  197. I added file saving. Here is a shar file with the files that I touched.
  198. I'd appreciate if this made it into the next release of xv.
  199.  
  200.                 Thanks, Thomas.
  201.  
  202.  
  203. # This is a shell archive.  Remove anything before this line,
  204. # then unpack it by saving it in a file and typing "sh file".
  205. #
  206. # Wrapped by  on Fri Nov 15 15:35:17 EST 1991
  207. # Contents:  xv.h xvdir.c xvvis.c
  208.  
  209. echo x - xv.h
  210. sed 's/^@//' > "xv.h" <<'@//E*O*F xv.h//'
  211. /*
  212.  *  xv.h  -  header file for xv, but you probably guessed as much
  213.  */
  214.  
  215. /*
  216.  * Copyright 1989, 1990 by the University of Pennsylvania
  217.  *
  218.  * Permission to use, copy, and distribute for non-commercial purposes,
  219.  * is hereby granted without fee, providing that the above copyright
  220.  * notice appear in all copies and that both the copyright notice and this
  221.  * permission notice appear in supporting documentation.
  222.  *
  223.  * The software may be modified for your own purposes, but modified versions
  224.  * may not be distributed.
  225.  *
  226.  * This software is provided "as is" without any express or implied warranty.
  227.  */
  228.  
  229. #define REVDATE   "Rev: 11/29/90  (Patchlevel 3)"
  230.  
  231. #ifdef SVR4             /* SysV release 4 uses dirent */
  232. #ifndef sgi             /* but Silicon Graphics doesn't */
  233. #define DIRENT
  234. #endif
  235. #endif
  236.  
  237.  
  238. /* include files */
  239. #include <stdio.h>
  240. #include <math.h>
  241. #include <ctype.h>
  242. #include <string.h>
  243. extern int   errno;             /* this SHOULD be in errno.h */
  244. extern char *sys_errlist[];     /* this SHOULD be in errno.h */
  245.  
  246. #ifndef __convexc__             /* Convex doesn't have <memory.h> */
  247. #include <memory.h>             /* for 'memset()' prototype */
  248. #endif
  249.  
  250. /* neither IBM AOS 4.3, Convex, nor BSD 4.3 on VAX have <malloc.h> */
  251. #if !defined(ibm032) && !defined(__convexc__) && \
  252.     !(defined(vax) && !defined(ultrix))
  253. #if defined(hp300) || defined(hp800)
  254. #include <sys/malloc.h>                /* it's in 'sys' on HPs*/
  255. #else
  256. #include <malloc.h>
  257. #endif
  258. #endif
  259.  
  260.  
  261. #if defined(NEED_MEMROUTINES)
  262. #define memcpy(d,s,l) bcopy(s,d,l)
  263. #endif
  264.  
  265.  
  266. #include <X11/Xos.h>
  267. #include <X11/Xlib.h>
  268. #include <X11/Xutil.h>
  269. #include <X11/cursorfont.h>
  270. #include <X11/keysym.h>
  271.  
  272.  
  273. #if defined(NEEDSTIME) || defined(NEEDSDIR)
  274. #include <sys/types.h>    /* only include <sys/types.h> once */
  275. #endif
  276.  
  277. #ifdef NEEDSTIME
  278. #ifndef sgi              /* silicon graphics doesn't have timeb.h */
  279. #include <sys/timeb.h>
  280. #endif
  281. #undef SIGCHLD
  282. #include <signal.h>
  283. #if defined(sco) && !defined(NOTIMER)
  284. #include <sys/itimer.h>
  285. #endif
  286. #ifndef  sigmask
  287. #define  sigmask(m)      (1 << ((m)-1))
  288. #endif
  289. #endif
  290.  
  291. #ifdef NEEDSDIR
  292. #ifndef HPUX
  293. #ifdef sco
  294. #include <sys/ndir.h>
  295. #define lstat stat
  296. #else
  297. #ifndef ATT
  298. #include <sys/dir.h>
  299. #endif  /* ATT */
  300. #endif  /* sco */
  301. #endif  /* not HPUX */
  302. #include <sys/param.h>
  303. #include <sys/stat.h>
  304. #ifdef DIRENT
  305. #include <dirent.h>
  306. #endif
  307. #endif
  308.  
  309. #ifdef NEEDSVARARGS
  310. #include <varargs.h>
  311. #endif
  312.  
  313. /* signal macros */
  314. #ifdef SVR4
  315. #define HOLD_SIG         sighold(SIGALRM)  /* block ALRM sig from occurring */
  316. #define RELEASE_SIG      sigrelse(SIGALRM) /* */
  317. #define PAUSE_SIG        sigpause(SIGALRM) /* sleep until ALRM signal */
  318. #else
  319. #define HOLD_SIG         sigblock(sigmask(SIGALRM))
  320. #define RELEASE_SIG      sigblock(0)
  321. #define PAUSE_SIG        sigpause(0)
  322. #endif
  323.  
  324.  
  325. #ifdef i386
  326. #define MAXPATHLEN    500
  327. #define UNCOMPRESS    "/usr/local/bin/uncompress"   /* uncompress program */
  328. #undef  HOLD_SIG
  329. #define HOLD_SIG      /* don't know how to handle signals  MWS 10/18/90 */
  330. #undef  RELEASE_SIG
  331. #define RELEASE_SIG   /* */
  332. #undef  PAUSE_SIG
  333. #define PAUSE_SIG     /* */
  334. #else
  335. #define UNCOMPRESS "/usr/ucb/uncompress"   /* for uncompressing .Z files */
  336. #endif
  337.  
  338. #define PROGNAME  "xv"             /* used in resource database */
  339.  
  340. #define DEFINFOGEOM "-10+10"       /* default position of info window */
  341. #define DEFDIRGEOM  "-10-10"       /* default position of directory window */
  342. #define DEFCTRLGEOM "+400+400"     /* default position of ctrl window */
  343. #define DEFGAMGEOM  "+10-10"       /* default position of gamma window */
  344.  
  345. #define INFOWIDE 500               /* (fixed) size of info window */
  346. #define INFOHIGH 250
  347.  
  348. #define CTRLWIDE 440               /* (fixed) size of control window */
  349. #define CTRLHIGH 295
  350.  
  351. #define DIRWIDE  300               /* (fixed) size of directory window */
  352. #define DIRHIGH  438
  353.  
  354. #define GAMWIDE  366               /* (fixed) size of Gamma window */
  355. #define GAMHIGH  356
  356.  
  357. #define MAXNAMES 1024   /* max # of files (more than this?  Get REAL!)
  358.  
  359. /* strings in the INFOBOX (used in SetISTR and GetISTR) */
  360. #define NISTR         9    /* number of ISTRs */
  361. #define ISTR_INFO     0
  362. #define ISTR_WARNING  1
  363. #define ISTR_FILENAME 2
  364. #define ISTR_FORMAT   3
  365. #define ISTR_RES      4
  366. #define ISTR_CROP     5
  367. #define ISTR_EXPAND   6
  368. #define ISTR_COLOR    7
  369. #define ISTR_COLOR2   8
  370.  
  371. /* potential values of 'infomode', used in info box drawing routines */
  372. #define INF_NONE 0    /* empty box */
  373. #define INF_STR  1    /* just ISTR_INFO */
  374. #define INF_PART 2    /* filename, format, size and infostr */
  375. #define INF_FULL 3    /* INF_PART + clipping, expansion, colorinfo */
  376.  
  377.  
  378. /* buttons in the ctrl window */
  379. #define NBUTTS  20
  380. #define BNEXT   0
  381. #define BPREV   1
  382. #define BCROP   2
  383. #define BUNCROP 3
  384. #define BNORM   4
  385. #define BMAX    5
  386. #define BUP2    6
  387. #define BDN2    7
  388. #define BUP10   8
  389. #define BDN10   9
  390. #define BQUIT   10
  391. #define B4BY3   11
  392. #define BSAVE   12
  393. #define BROTL   13
  394. #define BINFO   14
  395. #define BGAMMA  15
  396. #define BASPECT 16
  397. #define BROTR   17
  398. #define BMAXPECT 18
  399. #define BACROP   19
  400.  
  401. /* buttons in the 'save' window */
  402. #define S_NBUTTS 4
  403. #define S_BOPEN  0
  404. #define S_BSAVE  1
  405. #define S_BCANC  2
  406. #define S_BQUIT  3
  407.  
  408.  
  409. /* buttons in the 'gamma' window */
  410. #define G_NBUTTS  17
  411. #define G_BAPPLY  0
  412. #define G_BNOGAM  1
  413. #define G_BRESET  2
  414. #define G_BDEF    3
  415. #define G_BGTYPE  4
  416. #define G_BCLOSE  5
  417. #define G_BUP_BR  6
  418. #define G_BDN_BR  7
  419. #define G_BUP_CN  8
  420. #define G_BDN_CN  9
  421. #define G_BHSVRGB 10
  422. #define G_B1      11
  423. #define G_B2      12
  424. #define G_B3      13
  425. #define G_B4      14
  426. #define G_BSET    15
  427. #define G_BUNDO   16
  428.  
  429.  
  430. /* definitions of first char of dirnames[i] (filetype) */
  431. #define C_FIFO  'f'    /* FIFO special file */
  432. #define C_CHR   'c'    /* character special file */
  433. #define C_DIR   'd'    /* directory */
  434. #define C_BLK   'b'    /* block special file */
  435. #define C_LNK   'l'    /* symbolic link */
  436. #define C_SOCK  's'    /* socket */
  437. #define C_REG   ' '    /* regular file */
  438.  
  439.  
  440. /* random string-placing definitions */
  441. #define SPACING 3      /* vertical space between strings */
  442. #define ASCENT   (mfinfo->ascent)
  443. #define DESCENT  (mfinfo->descent)
  444. #define CHIGH    (ASCENT + DESCENT)
  445. #define LINEHIGH (CHIGH + SPACING)
  446.  
  447.  
  448. #define STDINSTR "<stdin>"
  449.  
  450.  
  451. #ifndef MAIN
  452. #define WHERE extern
  453. #else
  454. #define WHERE
  455. #endif
  456.  
  457. typedef unsigned char byte;
  458.  
  459. typedef struct { Window win;            /* window ID */
  460.          int len;               /* length of major axis */
  461.          int vert;              /* true if vertical, else horizontal */
  462.          int active;            /* true if scroll bar can do anything*/
  463.          int min,max;           /* min/max values 'pos' can take */
  464.          int val;               /* 'value' of scrollbar */
  465.          int page;              /* amt val change on pageup/pagedown */
  466.          int tpos;              /* thumb pos. (pixels from tmin) */
  467.          int tmin,tmax;         /* min/max thumb offsets (from 0,0) */
  468.          int tsize;             /* size of thumb (in pixels) */
  469.          unsigned long fg,bg;   /* colors */
  470.          void (*drawobj)();     /* redraws obj controlled by scrl*/
  471.          int uplit, dnlit;      /* true if up&down arrows are lit */
  472.            } SCRL;
  473.  
  474. typedef struct { Window win;            /* parent window */
  475.          int x,y,w,h;           /* size of button rectangle */
  476.          int lit;               /* if true, invert colors */
  477.          int active;            /* if false, stipple gray */
  478.          int toggle;            /* if true, clicking toggles state */
  479.          unsigned long fg,bg;   /* colors */
  480.          char *str;             /* string in button */
  481.            } BUTT;
  482.  
  483.  
  484. typedef struct { Window win;            /* window */
  485.          int x,y,w,h;           /* size of window */
  486.          unsigned long fg,bg;   /* colors */
  487.          char **str;            /* ptr to list of strings */
  488.          int   nstr;            /* number of strings */
  489.          int   selected;        /* number of 'selected' string */
  490.          int   nlines;          /* number of lines shown at once */
  491.          SCRL  scrl;            /* scrollbar that controls list */
  492.          int   filetypes;       /* true if filetype icons to be drawn*/
  493.          int   dirsonly;        /* if true, only dirs selectable */
  494.            } LIST;
  495.  
  496.  
  497. typedef struct rbutt { Window        win;      /* parent window */
  498.                int           x,y;      /* position in parent */
  499.                char         *str;      /* the message string */
  500.                int           selected; /* selected or not */
  501.                int           active;   /* selectable? */
  502.                struct rbutt *next;     /* pointer to next in group */
  503.                unsigned long fg,bg;    /* colors */
  504.              } RBUTT;
  505.  
  506. /* MACROS */
  507. #define CENTERX(f,x,str) ((x)-XTextWidth(f,str,strlen(str))/2)
  508. #define CENTERY(f,y) ((y)-((f->ascent+f->descent)/2)+f->ascent)
  509.  
  510. /* RANGE forces a to be in the range b..c (inclusive) */
  511. #define RANGE(a,b,c) { if (a<b) a=b;  if (a>c) a=c; }
  512.  
  513. /* PTINRECT returns '1' if x,y is in rect (inclusive) */
  514. #define PTINRECT(x,y,rx,ry,rw,rh) \
  515.            ((x)>=(rx) && (y)>=(ry) && (x)<=(rx)+(rw) && (y)<=(ry)+(rh))
  516.  
  517. /* MONO returns total intensity of r,g,b components */
  518. #define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 5)  /*.33R+ .5G+ .17B*/
  519.  
  520.  
  521.  
  522. /* X stuff */
  523. WHERE Display       *theDisp;
  524. WHERE int           theScreen;
  525. WHERE unsigned int  ncells, dispWIDE, dispHIGH, dispDEEP;
  526. WHERE Colormap      theCmap, LocalCmap;
  527. WHERE Window        rootW, mainW;
  528. WHERE GC            theGC;
  529. WHERE unsigned long black, white, fg, bg, infofg, infobg;
  530. WHERE Font          mfont, monofont;
  531. WHERE XFontStruct   *mfinfo, *monofinfo;
  532. WHERE Visual        *theVisual;
  533. WHERE Cursor        arrow, cross;
  534. WHERE Pixmap        iconPix;
  535.  
  536. /* global vars used by LOAD routines */
  537. WHERE byte          *pic;                   /* ptr to loaded picture */
  538. WHERE unsigned int   pWIDE,pHIGH;           /* size of 'pic' */
  539. WHERE byte           r[256],g[256],b[256];  /* colormap */
  540. WHERE char          *cmd;                   /* program name for printf's */
  541. WHERE int            DEBUG;                 /* print debugging info */
  542. WHERE int            mono;                  /* true if displaying grayscale */
  543.  
  544.  
  545. /* more global variables, used by xv and xvmisc */
  546. WHERE byte          *cpic;         /* cropped version of pic */
  547. WHERE unsigned int  cWIDE, cHIGH,  /* size of cropped region */
  548.                     cXOFF, cYOFF;  /* offset of region from 0,0 of pic */
  549.  
  550. WHERE byte          *epic;         /* expanded version of cpic */
  551.                                    /* points to pic when at 1:1 expansion */
  552.                                    /* this is converted to 'theImage' */
  553. WHERE unsigned int  eWIDE, eHIGH;  /* size of epic */
  554. WHERE unsigned int  normFact;      /* factor to shrink picture by for 'norm' */
  555.  
  556. WHERE byte           rorg[256],gorg[256],borg[256];  /* ORIGINAL colormap */
  557. WHERE byte           gamcr[256];   /* gamma correction curve */
  558. WHERE byte           fsgamcr[256]; /* gamma correction curve (for FS dither) */
  559.  
  560.  
  561. WHERE XImage        *theImage;     /* X version of epic */
  562.  
  563.  
  564. WHERE unsigned long freecols[256]; /* list of pixel values to free */
  565. WHERE int           nfcols;        /* number of colors to free */
  566. WHERE unsigned long cols[256];     /* maps pic pixel values to X pixel vals */
  567. WHERE int           fc2pcol[256];  /* maps freecols into pic pixel values */
  568. WHERE int           numcols;       /* # of desired colors in picture */
  569. WHERE int           ncols;         /* max # of (different) colors to alloc */
  570.  
  571. WHERE char          str[128];      /* dummy string used for error messages */
  572.  
  573. WHERE int           expand,        /* expansion amount */
  574.                     bwidth,        /* border width of created windows */
  575.                     noglob,        /* force to only use colors it alloced */
  576.                     revvideo,      /* reverse video */
  577.                     perfect,       /* perfect color.  install own colormap */
  578.                     fixedaspect,   /* fixed aspect ratio */
  579.                     slow24,        /* use slow 24to8 algorithm */
  580.                     ninstall,      /* true if using icccm-complaint WM
  581.                       (a WM that will does install CMaps */
  582.                     useroot,       /* true if we should draw in rootW */
  583.                     noqcheck,      /* true if we should NOT do QuickCheck */
  584.                     rwcolor,       /* true if we should use R/W color cells */
  585.                     rwthistime,    /* true if we DID use R/W color cells */
  586.                     fish,          /* turn on annoying fish */
  587.                     fishrunning;   /* true if fish are in operation */
  588.  
  589. WHERE float         defaspect,     /* default aspect ratio to use */
  590.                     normaspect;    /* normal aspect ratio of this picture */
  591.  
  592. WHERE int           crx1, cry1,    /* dimensions of cropping rectangle */
  593.                     crx2, cry2;
  594.  
  595.  
  596.  
  597. /* stuff used for 'info' box */
  598. WHERE Window        infoW;
  599. WHERE int           infoUp;       /* boolean:  whether infobox is visible */
  600. WHERE int           infoMode;
  601.  
  602.  
  603. /* stuff used for 'ctrl' box */
  604. WHERE Window        ctrlW;
  605. WHERE int           ctrlUp;       /* boolean:  whether ctrlbox is visible */
  606. WHERE char         *namelist[MAXNAMES];  /* list of file names from argv */
  607. WHERE char         *dispnames[MAXNAMES]; /* truncated names shown in listbox */
  608. WHERE int           numnames, curname;
  609. WHERE LIST          nList;
  610. WHERE BUTT          but[NBUTTS];         /* command buttons in ctrl window */
  611. WHERE Pixmap        grayTile, grayStip;  /* for drawing dim things */
  612.  
  613. /* stuff used for 'directory' box */
  614. WHERE Window        dirW, ddirW, dnamW;
  615. WHERE int           dirUp;       /* is dirW mapped or not */
  616. WHERE LIST          dList;       /* list of filenames in current directory */
  617. WHERE BUTT          dbut[S_NBUTTS];
  618.  
  619. /* stuff used for 'gamma' box */
  620. #define NUMHANDS 4
  621. WHERE Window        gamW,graphW;
  622. WHERE int           gamUp;       /* is gamW mapped or not */
  623. WHERE BUTT          gbut[G_NBUTTS];
  624. WHERE XPoint        ghand[NUMHANDS];
  625.  
  626. #undef WHERE
  627.  
  628.  
  629.  
  630.  
  631.  
  632. /* function declarations for externally-callable functions */
  633.  
  634. #ifdef __STDC__ 
  635. /****************************** XV.C ****************************/
  636. void DrawWindow(int, int, int, int);
  637. void WCrop(int, int);
  638. void WUnCrop(void);
  639. void WResize(int, int);
  640. void WRotate(void);
  641. void InvCropRect(void);
  642. void MakeRootPic(void);
  643. void XvFreeColors(Display*, Colormap, unsigned long*, int, unsigned long, int);
  644.  
  645. /*************************** XVMISC.C ***************************/
  646. Window CreateWindow(char *, char *, unsigned int, unsigned int, 
  647.             unsigned long, unsigned long);
  648. void Resize(int, int);
  649. void Rotate(int);
  650. void SortColormap(void);
  651. void AllocColors(void);
  652. void AllocRWColors(void);
  653. void DoMonoAndRV(void);
  654. void DoCrop(void);
  655. void UnCrop(void);
  656. void AutoCrop(void);
  657. void FSDither(byte *, int, int, byte *);
  658. void CreateXImage(void);
  659. void CenterString(Window, char *, int, int);
  660. void ULineString(Window, char *, int, int);
  661. int  StringWidth(char *);
  662. void FakeButtonPress(BUTT *);
  663. void SetCropString(void);
  664. void Warning(void);
  665. void FatalError(char *);
  666. void LoadFishCursors(void);
  667. void SetCursors(int);
  668. void WaitCursor(void);
  669. void Quit(int);
  670. void Timer(int);
  671.  
  672. /*************************** XV24TO8.C **************************/
  673. int  Conv24to8(byte *, int, int, int);
  674. void InitFSDTables(void);
  675.  
  676. /**************************** XVCTRL.C **************************/
  677. void CreateCtrl(char *);
  678. void CtrlBox(int);
  679. void RedrawCtrl(int, int, int, int);
  680. int  ClickCtrl(int, int);
  681. void DrawCtrlStr(void);
  682. void ScrollToCurrent(void);
  683.  
  684. void LSCreate(LIST *, Window, int, int, int, int, int, char **, int, 
  685.           unsigned long, unsigned long, void (*)(void), int, int);
  686. void LSRedraw(LIST *);
  687. int  LSClick (LIST *, XButtonEvent *);
  688. void LSNewData(LIST *, char **, int);
  689.  
  690.  
  691. /*************************** XVINFO.C ***************************/
  692. void  CreateInfo(char *);
  693. void  InfoBox(int);
  694. void  RedrawInfo(int, int, int, int);
  695. void  SetInfoMode(int);
  696. void  SetISTR(int, ...);
  697. char *GetISTR(int);
  698.  
  699. /**************************** XVDIR.C ***************************/
  700. void CreateDirW(char *);
  701. void DirBox(int);
  702. void RedrawDirW(int,int,int,int);
  703. int  ClickDirW(int, int);
  704. void LoadCurrentDirectory(void);
  705. void RedrawDDirW(void);
  706. void RedrawDNamW(void);
  707. void SelectDir(int);
  708. void DirOpenActive(void);
  709. void TrackDDirW(int,int);
  710. int  DirKey(int);
  711. int  DoSave(void);
  712. void SetDirFName(char *);
  713.  
  714.  
  715. /**************************** XVGAM.C **************************/
  716. void CreateGam(char *);
  717. void GamBox(int);
  718. void RedrawGam(int, int, int, int);
  719. void RedrawGraph(int, int, int, int);
  720. void ClickGam(int, int);
  721. void TrackGraph(int, int);
  722. void GenerateGamma(void);
  723. void GenerateFSGamma(void);
  724. void GammifyColors(void);
  725. void SetGPreset(int, int, int, int, int, int, int);
  726.  
  727. /*************************** XVSCRL.C ***************************/
  728. void SCCreate  (SCRL *, Window, int, int, int, int, int, int, int, int, 
  729.                       unsigned long, unsigned long, void (*)(void));
  730. void SCSetRange(SCRL *, int, int, int, int);
  731. void SCSetVal  (SCRL *, int);
  732. void SCRedraw  (SCRL *);
  733. void SCTrack   (SCRL *, int, int);
  734.  
  735.  
  736. /**************************** XVBUTT.C ***************************/
  737.  
  738. void BTCreate(BUTT *, Window, int, int, int, int, char *, 
  739.           unsigned long, unsigned long);
  740. void BTSetActive(BUTT *, int);
  741. void BTRedraw(BUTT *);
  742. int  BTTrack (BUTT *);
  743.  
  744.  
  745. RBUTT *RBCreate(RBUTT *, Window, int, int, char*, 
  746.         unsigned long, unsigned long);
  747. void   RBRedraw(RBUTT *, int);
  748. void   RBSelect(RBUTT *, int);
  749. int    RBWhich(RBUTT *);
  750. int    RBCount(RBUTT *);
  751. void   RBSetActive(RBUTT *, int, int);
  752. int    RBClick(RBUTT *, int, int);
  753. void   RBTrack(RBUTT *, int);
  754.  
  755.  
  756. /**************************** XVGIF.C ***************************/
  757. int LoadGIF(char *, int);
  758.  
  759. /*************************** XVGIFWR.C **************************/
  760. int WriteGIF(FILE *, byte *, int, int, byte *, byte *, byte *, int, int);
  761.  
  762. /**************************** XVPM.C ****************************/
  763. int LoadPM(char *, int);
  764. int WritePM(FILE *, byte *, int, int, byte *, byte *, byte *, int, int);
  765.  
  766. /**************************** XVPBM.C ***************************/
  767. int LoadPBM(char *, int);
  768. int WritePBM(FILE *, byte *, int, int, byte *, byte *, byte *, int, int, int);
  769.  
  770. /**************************** XVXBM.C ***************************/
  771. int LoadXBM(char *, int);
  772. int WriteXBM(FILE *, byte *, int, int, char *);
  773.  
  774. /**************************** XVJPEG.C ***************************/
  775. int LoadJFIF(char *, int);
  776.  
  777.  
  778.  
  779.  
  780. #else     /* using non-ANSI cc.  Function defs, but no params */
  781.  
  782.  
  783.  
  784.  
  785. /****************************** XV.C ****************************/
  786. void DrawWindow(), WCrop(), WUnCrop(), WResize(), WRotate(), InvCropRect();
  787. void MakeRootPic();
  788. void XvFreeColors();
  789.  
  790. /*************************** XVMISC.C ***************************/
  791. Window CreateWindow();
  792. void   Resize(), Rotate(), SortColormap(), AllocColors(), DoCrop(), UnCrop();
  793. void   AutoCrop(), DoMonoAndRV();
  794. void   AllocRWColors(), FSDither(), CenterString(), ULineString();
  795. int    StringWidth();
  796. void   FakeButtonPress(), SetCropString(), Warning(), FatalError(), Quit();
  797. void   Timer(), CreateXImage(), LoadFishCursors(), SetCursors(), WaitCursor();
  798.  
  799. /*************************** XV24TO8.C **************************/
  800. int  Conv24to8();
  801. void InitFSDTables();
  802.  
  803. /**************************** XVCTRL.C **************************/
  804. void CreateCtrl(), CtrlBox(), RedrawCtrl(), DrawCtrlStr(), ScrollToCurrent();
  805. int  ClickCtrl();
  806.  
  807. void LSCreate(), LSRedraw(), LSNewData();
  808. int  LSClick();
  809.  
  810. /*************************** XVINFO.C ***************************/
  811. void  CreateInfo(), InfoBox(), RedrawInfo(), SetInfoMode(), SetISTR();
  812. char *GetISTR();
  813.  
  814. /**************************** XVDIR.C ***************************/
  815. void CreateDirW(), DirBox(), RedrawDirW(), LoadCurrentDirectory();
  816. int  ClickDirW(), DoSave(), DirKey();
  817. void RedrawDDirW(), RedrawDNamW(), SelectDir(), DirOpenActive(), TrackDDirW();
  818. void SetDirFName();
  819.  
  820. /**************************** XVGAM.C **************************/
  821. void CreateGam(), GamBox(), RedrawGam(), RedrawGraph(), ClickGam();
  822. void TrackGraph(), GenerateGamma(), GenerateFSGamma(), GammifyColors();
  823. void SetGPreset();
  824.  
  825. /*************************** XVSCRL.C ***************************/
  826. void SCCreate(), SCSetRange(), SCSetVal(), SCRedraw(), SCTrack();
  827.  
  828. /**************************** XVBUTT.C ***************************/
  829. void BTCreate(), BTSetActive(), BTRedraw();
  830. int  BTTrack();
  831.  
  832. RBUTT *RBCreate();
  833. void   RBRedraw(), RBSelect(), RBSetActive(), RBTrack();
  834. int    RBWhich(), RBCount(), RBClick();
  835.  
  836. /**************************** XVGIF.C ***************************/
  837. int LoadGIF();
  838.  
  839. /*************************** XVGIFWR.C **************************/
  840. int WriteGIF();
  841.  
  842. /**************************** XVPM.C ****************************/
  843. int LoadPM(), WritePM();
  844.  
  845. /**************************** XVPBM.C ***************************/
  846. int LoadPBM(), WritePBM();
  847.  
  848. /**************************** XVXBM.C ***************************/
  849. int LoadXBM(), WriteXBM();
  850.  
  851. /**************************** XVJPEG.C ***************************/
  852. int LoadJFIF();
  853.  
  854. #endif
  855. @//E*O*F xv.h//
  856. chmod u=rw,g=r,o=r xv.h
  857.  
  858. echo x - xvdir.c
  859. sed 's/^@//' > "xvdir.c" <<'@//E*O*F xvdir.c//'
  860. /* 
  861.  * xvdir.c - Directory changin', file i/o dialog box
  862.  *
  863.  * callable functions:
  864.  *
  865.  *   CreateDirW(geom,bwidth)-  creates the dirW window.  Doesn't map it.
  866.  *   DirBox(vis)            -  random processing based on value of 'vis'
  867.  *                             maps/unmaps window, etc.
  868.  *   RedrawDirW(x,y,w,h)    -  called by 'expose' events
  869.  *   ClickDirW()            -  handles mouse clicks in DirW
  870.  *   LoadCurrentDirectory() -  loads up current dir information for dirW
  871.  *   DoSave()               -  calls appropriate save routines
  872.  *   SetDirFName()          -  sets the 'save-as' filename 
  873.  */
  874.  
  875. /*
  876.  * Copyright 1989, 1990 by the University of Pennsylvania
  877.  *
  878.  * Permission to use, copy, and distribute for non-commercial purposes,
  879.  * is hereby granted without fee, providing that the above copyright
  880.  * notice appear in all copies and that both the copyright notice and this
  881.  * permission notice appear in supporting documentation.
  882.  *
  883.  * The software may be modified for your own purposes, but modified versions
  884.  * may not be distributed.
  885.  *
  886.  * This software is provided "as is" without any express or implied warranty.
  887.  */
  888.  
  889.  
  890. #define NEEDSDIR
  891. #include "xv.h"
  892.  
  893. #define NLINES 9                   /* # of lines in list control (keep odd) */
  894. #define LISTW  200
  895.  
  896. #define BUTTW   60
  897. #define BUTTH   19
  898. #define DDWIDE  LISTW-80+15
  899.  
  900. #define MAXDEEP 30    /* maximum number of directories in cwd path */
  901. #define MAXFNLEN 40   /* max length of filename being entered */
  902.  
  903. #define DEFFILENAME ""   /* default filename filled in when program starts */
  904.  
  905. #ifdef __STDC__
  906. static void RedrawDList(void);
  907. static int  dnamcmp(char **, char **);
  908. #else
  909. static void RedrawDList();
  910. static int  dnamcmp();
  911. #endif
  912.  
  913. static int    listh;
  914. static char  *dirnames[MAXNAMES];
  915. static int    numdirnames = 0, ndirs = 0;
  916. static char   path[MAXPATHLEN+1];
  917. static char  *dirs[MAXDEEP];            /* list of directory names */
  918. static char  *lastdir;                  /* name of the directory we're in */
  919. static char   filename[MAXFNLEN];       /* filename being entered */
  920. static RBUTT *formatRB, *colorRB, *sizeRB;
  921.  
  922.  
  923. /***************************************************/
  924. void CreateDirW(geom)
  925. char *geom;
  926. {
  927.   int y;
  928.  
  929.   listh = LINEHIGH * NLINES;
  930.  
  931.   dirW = CreateWindow("xv save dialog",geom,DIRWIDE, DIRHIGH, infofg, infobg);
  932.   if (!dirW) FatalError("couldn't create 'save' window!");
  933.  
  934.   /* create doo-wah's */
  935.   ddirW = XCreateSimpleWindow(theDisp, dirW, 40, 5, DDWIDE, LINEHIGH,
  936.                   2, infofg, infobg);
  937.   if (!ddirW) FatalError("can't create path window");
  938.   XSelectInput(theDisp, ddirW, ExposureMask | ButtonPressMask);
  939.  
  940.   dnamW = XCreateSimpleWindow(theDisp, dirW, 80, listh+75-ASCENT-4, 
  941.                   200, LINEHIGH+4, 1, infofg, infobg);
  942.   if (!dnamW) FatalError("can't create name window");
  943.   XSelectInput(theDisp, dnamW, ExposureMask);
  944.  
  945.  
  946.   LSCreate(&dList, dirW, 10, 14+LINEHIGH, LISTW, listh, NLINES,
  947.        dirnames, numdirnames,
  948.        infofg, infobg, RedrawDList, 1, 0);
  949.  
  950.   BTCreate(&dbut[S_BOPEN], dirW, 233, dList.y-9+listh/5, 60, BUTTH, 
  951.        "Open", infofg, infobg);
  952.   BTCreate(&dbut[S_BSAVE], dirW, 233, dList.y-9+(listh*2)/5, 60, BUTTH, 
  953.        "Save", infofg, infobg);
  954.   BTCreate(&dbut[S_BCANC], dirW, 233, dList.y-9+(listh*3)/5, 60, BUTTH, 
  955.        "Cancel", infofg, infobg);
  956.   BTCreate(&dbut[S_BQUIT], dirW, 233, dList.y-9+(listh*4)/5, 60, BUTTH, 
  957.        "Quit", infofg, infobg);
  958.  
  959.   y = listh + 110;
  960.   formatRB = RBCreate(NULL, dirW, 26, y, "GIF", infofg, infobg);
  961.   RBCreate(formatRB, dirW, 26, y+18, "PM", infofg, infobg);
  962.   RBCreate(formatRB, dirW, 26, y+36, "PBM (raw)", infofg, infobg);
  963.   RBCreate(formatRB, dirW, 26, y+54, "PBM (ascii)", infofg, infobg);
  964.   RBCreate(formatRB, dirW, 26, y+72, "X11 Bitmap", infofg, infobg);
  965.   RBCreate(formatRB, dirW, 26, y+90, "VIS", infofg, infobg);
  966.  
  967.   colorRB = RBCreate(NULL, dirW, DIRWIDE/2, y, "Full Color", infofg, infobg);
  968.   RBCreate(colorRB, dirW, DIRWIDE/2, y+18, "Greyscale", infofg, infobg);
  969.   RBCreate(colorRB, dirW, DIRWIDE/2, y+36, "B/W Dithered", infofg, infobg);
  970.  
  971.   y = y + 133;
  972.   sizeRB = RBCreate(NULL, dirW, 26, y, "Normal Size", infofg, infobg);
  973.   RBCreate(sizeRB, dirW, 26, y+18, "At Current Expansion", infofg, infobg);
  974.  
  975.   SetDirFName(DEFFILENAME);
  976.  
  977.   LoadCurrentDirectory();
  978.  
  979.   XMapSubwindows(theDisp, dirW);
  980. }
  981.   
  982.  
  983. /***************************************************/
  984. void DirBox(vis)
  985. int vis;
  986. {
  987.   if (vis) XMapRaised(theDisp, dirW);
  988.   else     XUnmapWindow(theDisp, dirW);
  989.  
  990.   BTSetActive(&but[BSAVE], !vis);
  991.  
  992.   dirUp = vis;
  993. }
  994.  
  995.  
  996. /***************************************************/
  997. void RedrawDirW(x,y,w,h)
  998. int x,y,w,h;
  999. {
  1000.   int  i,ypos;
  1001.   char foo[30];
  1002.   XRectangle xr;
  1003.  
  1004.   xr.x = x;  xr.y = y;  xr.width = w;  xr.height = h;
  1005.   XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
  1006.  
  1007.   if (dList.nstr==1) strcpy(foo,"1 file");
  1008.                 else sprintf(foo,"%d files",dList.nstr);
  1009.  
  1010.   ypos = dList.y + dList.h + 5 + ASCENT;
  1011.   XSetForeground(theDisp, theGC, infobg);
  1012.   XFillRectangle(theDisp, dirW, theGC, 10, ypos-ASCENT, DIRWIDE, CHIGH);
  1013.   XSetForeground(theDisp, theGC, infofg);
  1014.   XDrawString(theDisp, dirW, theGC, 10, ypos, foo, strlen(foo));
  1015.  
  1016.   XDrawString(theDisp, dirW, theGC, 10, dList.h+75, "File name:",10);
  1017.  
  1018.   for (i=0; i<S_NBUTTS; i++) BTRedraw(&dbut[i]);
  1019.  
  1020.   RBRedraw(formatRB, -1);
  1021.   RBRedraw(colorRB, -1);
  1022.   RBRedraw(sizeRB, -1);
  1023.  
  1024.   ULineString(dirW, "Format", formatRB->x-16, formatRB->y-3-DESCENT);
  1025.   ULineString(dirW, "Colors", colorRB->x-16,  colorRB->y-3-DESCENT);
  1026.   ULineString(dirW, "Size",   sizeRB->x-16,   sizeRB->y-3-DESCENT);
  1027.  
  1028.   XSetClipMask(theDisp, theGC, None);
  1029. }
  1030.  
  1031.  
  1032. /***************************************************/
  1033. void RedrawDDirW()
  1034. {
  1035.   XSetForeground(theDisp, theGC, infofg);
  1036.   XSetBackground(theDisp, theGC, infobg);
  1037.  
  1038.   XClearWindow(theDisp, ddirW);
  1039.   XDrawString(theDisp, ddirW, theGC, 3, ASCENT + 1,
  1040.               lastdir, strlen(lastdir));
  1041. }
  1042.  
  1043.  
  1044. /***************************************************/
  1045. int ClickDirW(x,y)
  1046. int x,y;
  1047. {
  1048.   BUTT  *bp;
  1049.   int    bnum;
  1050.  
  1051.  
  1052.   /* check the RBUTTS first, since they don't DO anything */
  1053.   if ( (bnum=RBClick(formatRB, x,y)) >= 0) { 
  1054.     RBTrack(formatRB, bnum);
  1055.     if (RBWhich(formatRB)==4) {  /* turn off FULLCOLOR + GRAYSCALE */
  1056.       RBSetActive(colorRB,0,0);
  1057.       RBSetActive(colorRB,1,0);
  1058.       RBSelect(colorRB,2);
  1059.     }
  1060.     else {                       /* turn on FULLCOLOR + GRAYSCALE */
  1061.       RBSetActive(colorRB,0,1);
  1062.       RBSetActive(colorRB,1,1);
  1063.     }
  1064.     return -1;
  1065.   }
  1066.  
  1067.   if ( (bnum=RBClick(colorRB, x,y)) >= 0) 
  1068.     { RBTrack(colorRB, bnum);  return -1; }
  1069.  
  1070.   if ( (bnum=RBClick(sizeRB, x,y)) >= 0) 
  1071.     { RBTrack(sizeRB, bnum);  return -1; }
  1072.  
  1073.   for (bnum=0; bnum<S_NBUTTS; bnum++) {
  1074.     bp = &dbut[bnum];
  1075.     if (PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
  1076.   }
  1077.  
  1078.   if (bnum<S_NBUTTS) {   /* found one */
  1079.     if (BTTrack(bp)) return (bnum);
  1080.   }
  1081.  
  1082.   return -1;
  1083. }
  1084.  
  1085.  
  1086. /***************************************************/
  1087. void SelectDir(n)
  1088. int n;
  1089. {
  1090.   int pend;
  1091.  
  1092.   /* called when entry #n in the dir list was selected/double-clicked */
  1093.  
  1094.   if (dList.str[n][0] == C_DIR || 
  1095.       dList.str[n][0] == C_LNK) {  /* it's cool, it's (possibly) a directory */
  1096.     pend = strlen(path);
  1097.     strcat(path,dList.str[n]+1);   /* add to pathname */
  1098.     if (chdir(path)) {
  1099.       fprintf(stderr,"unable to cd to '%s'\n",path);
  1100.       path[pend] = '\0';           /* undo path modification */
  1101.     }
  1102.     else 
  1103.       LoadCurrentDirectory();
  1104.   }
  1105.  
  1106.   else {  /* not a directory */
  1107.     /* copy the clicked-on filename into the 'save-as' filename */
  1108.     SetDirFName(dList.str[n]+1);
  1109.   }
  1110. }
  1111.  
  1112.  
  1113. /***************************************************/
  1114. void TrackDDirW(x,y)
  1115. int x,y;
  1116. {
  1117.   Window        menuW, rW, cW;
  1118.   int           rx, ry, i,j, sel, lastsel;
  1119.   unsigned int  mask;
  1120.  
  1121.   XSetForeground(theDisp, theGC, infofg);
  1122.   XSetBackground(theDisp, theGC, infobg);
  1123.  
  1124.   menuW = XCreateSimpleWindow(theDisp, dirW, 40, 5, DDWIDE, ndirs*LINEHIGH,
  1125.                   2, infofg, infobg);
  1126.   if (!menuW) FatalError("can't create path window");
  1127.  
  1128.   XMapRaised(theDisp, menuW);
  1129.  
  1130.   for (i=ndirs-1, j=0; i>=0; i--,j++)
  1131.     XDrawString(theDisp, menuW, theGC, 3, j*LINEHIGH + ASCENT + 1,
  1132.         dirs[i], dirs[i+1]-dirs[i]);
  1133.  
  1134.   XFlush(theDisp);
  1135.   XSetFunction(theDisp, theGC, GXinvert);
  1136.   XSetPlaneMask(theDisp, theGC, infofg ^ infobg);
  1137.   lastsel = -1;  sel = 0;
  1138.  
  1139.   while (XQueryPointer(theDisp, menuW, &rW, &cW, &rx, &ry, &x, &y, &mask)) {
  1140.     if (!(mask & Button1Mask)) break;
  1141.  
  1142.     /* see if mouse has left window */
  1143.  
  1144.     sel = y / LINEHIGH;
  1145.     if (sel>=ndirs) sel = ndirs-1;
  1146.     if (sel<0) sel = 0;
  1147.     if (sel != lastsel) {
  1148.       XFillRectangle(theDisp,menuW,theGC,0,lastsel*LINEHIGH,DDWIDE,LINEHIGH);
  1149.       XFillRectangle(theDisp,menuW,theGC,0,sel*LINEHIGH,DDWIDE,LINEHIGH);
  1150.       lastsel = sel;
  1151.     }
  1152.   }
  1153.  
  1154.   XSetFunction(theDisp, theGC, GXcopy);
  1155.   XSetPlaneMask(theDisp, theGC, AllPlanes);
  1156.   XDestroyWindow(theDisp, menuW);
  1157.  
  1158.   if (sel!=0) { /* changed directories */
  1159.     /* end 'path' by changing trailing '/' (of dir name) to a '\0' */
  1160.     *(dirs[(ndirs-1)-sel + 1] - 1) = '\0';
  1161.  
  1162.     /* special case:  if cd to '/', fix path (it's currently "") */
  1163.     if (path[0] == '\0') strcpy(path,"/");
  1164.  
  1165.     if (chdir(path)) {
  1166.       fprintf(stderr,"unable to cd to '%s'\n",path);
  1167.     }
  1168.     else 
  1169.       LoadCurrentDirectory();   
  1170.   }
  1171. }
  1172.  
  1173.  
  1174. /***************************************************/
  1175. static void RedrawDList()
  1176. {
  1177.   LSRedraw(&dList);
  1178. }
  1179.  
  1180.  
  1181. /***************************************************/
  1182. void LoadCurrentDirectory()
  1183. {
  1184.   DIR           *dirp;
  1185. #ifdef DIRENT
  1186.   struct dirent *dp;
  1187. #else
  1188.   struct direct *dp;
  1189. #endif
  1190.   int            i, ftype;
  1191.   struct stat    st;
  1192.   char          *dbeg, *dend;
  1193.  
  1194.   /* get rid of previous file names */
  1195.   for (i=0; i<numdirnames; i++) free(dirnames[i]);
  1196.  
  1197.   numdirnames = 0;
  1198.  
  1199. #ifdef SVR4
  1200.   getcwd(path, sizeof(path));
  1201. #else
  1202.   getwd(path);
  1203. #endif
  1204.   if (path[strlen(path)-1] != '/')
  1205.     strcat(path,"/");   /* tack on a trailing '/' to make path consistent */
  1206.  
  1207.   /* path will be something like: "/u3/bradley/src/weiner/whatever/" */
  1208.   /* parse path into individual directory names */
  1209.   dbeg = dend = path;
  1210.   for (i=0; i<MAXDEEP && dend; i++) {
  1211.     dend = strchr(dbeg,'/');  /* find next '/' char */
  1212.     dirs[i] = dbeg;
  1213.     dbeg = dend+1;
  1214.   }
  1215.   ndirs = i-1;
  1216.  
  1217.   lastdir = dirs[ndirs-1];
  1218.   RedrawDDirW();
  1219.  
  1220.   dirp = opendir(".");
  1221.   if (!dirp) {
  1222.     fprintf(stderr,"unable to open current directory");
  1223.     return;
  1224.   }
  1225.  
  1226.   i=0;
  1227.   while ( (dp = readdir(dirp)) != NULL) {
  1228.     if (strcmp(dp->d_name, ".")==0 || strcmp(dp->d_name, "..")==0) {
  1229.       /* skip over '.' and '..' */
  1230.     }
  1231.     else {
  1232. #ifdef DIRENT
  1233. #ifdef i386
  1234.       /* Not 100% sure d_reclen is correct, but it works...  MWS 10/18/90 */
  1235.       dirnames[i] = (char *) malloc(dp->d_reclen + 2); /* filetype + '\0'*/
  1236. #else
  1237.       dirnames[i] = (char *) malloc(strlen(dp->d_name) + 3);
  1238. #endif
  1239. #else
  1240.       dirnames[i] = (char *) malloc(dp->d_namlen + 2); /* +2=filetype + '\0'*/
  1241. #endif
  1242.       if (!dirnames[i]) FatalError("malloc error while reading directory");
  1243.       strcpy(dirnames[i]+1, dp->d_name);
  1244.  
  1245.       /* figure out what type of file the beastie is */
  1246.       dirnames[i][0] = C_REG;   /* default to normal file, if lstat fails */
  1247.  
  1248. #if defined(i386) || defined (SYSV)
  1249.       if (stat(dirnames[i]+1, &st)==0) {
  1250. #else
  1251.       if (lstat(dirnames[i]+1, &st)==0) {
  1252. #endif
  1253.     ftype = st.st_mode & S_IFMT;   /* mask off uninteresting bits */
  1254.     if      (ftype == S_IFDIR)  dirnames[i][0] = C_DIR;
  1255.     else if (ftype == S_IFCHR)  dirnames[i][0] = C_CHR;
  1256.     else if (ftype == S_IFBLK)  dirnames[i][0] = C_BLK;
  1257.  
  1258. #ifdef S_IFIFO
  1259.     else if (ftype == S_IFIFO)  dirnames[i][0] = C_FIFO;
  1260. #endif
  1261.  
  1262. #ifdef S_IFLNK
  1263.     else if (ftype == S_IFLNK)  dirnames[i][0] = C_LNK;
  1264. #endif
  1265.  
  1266. #ifdef S_IFSOCK
  1267.         else if (ftype == S_IFSOCK) dirnames[i][0] = C_SOCK;
  1268. #endif
  1269.       }
  1270.       else {
  1271.     /* fprintf(stderr,"problems 'stat-ing' files\n");*/
  1272.     dirnames[i][0] = C_REG;
  1273.       }
  1274.       i++;
  1275.     }
  1276.   }
  1277.  
  1278.   closedir(dirp);
  1279.  
  1280.   numdirnames = i;
  1281.  
  1282.   qsort((char *) dirnames, numdirnames, sizeof(char *), dnamcmp);
  1283.   LSNewData(&dList, dirnames, numdirnames);
  1284.   DirOpenActive();
  1285.   RedrawDirW(0,0,DIRWIDE,DIRHIGH);
  1286. }
  1287.  
  1288.  
  1289. /***************************************************/
  1290. static int dnamcmp(s1,s2)
  1291. char **s1, **s2;
  1292. {
  1293.   /* sort so that directories are at beginning of list */
  1294.  
  1295.   /* if both dirs or both not dirs, sort on name */
  1296.   if ( (**s1 == C_DIR && **s2 == C_DIR) || (**s1 != C_DIR && **s2 != C_DIR))
  1297.     return (strcmp((*s1)+1, (*s2)+1));
  1298.  
  1299.   else if (**s1==C_DIR) return -1;  /* s1 is first */
  1300.   else return 1;                    /* s2 is first */
  1301. }
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307. /***************************************************/
  1308. int DirKey(c)
  1309. int c;
  1310. {
  1311.   /* got keypress in dirW.  stick on end of filename */
  1312.   int len;
  1313.  
  1314.   len = strlen(filename);
  1315.   
  1316.   if (c>' ' && c<'\177') {              /* printable characters */
  1317.     if (c=='/') return(-1);             /* no directories in filename */
  1318.     if (len >= MAXFNLEN-1) return(-1);  /* max length of string */
  1319.     filename[len]=c;  filename[len+1]='\0';
  1320.   }
  1321.  
  1322.   else if (c=='\010' || c=='\177') {    /* BS or DEL */
  1323.     if (len==0) return(-1);             /* string already empty */
  1324.     filename[len-1]='\0';
  1325.   }
  1326.  
  1327.   else if (c=='\025' || c=='\013') {    /* ^U or ^K clear line */
  1328.     filename[0] = '\0';
  1329.   }
  1330.  
  1331.   else if (c=='\012' || c=='\015') {    /* CR or LF */
  1332.     FakeButtonPress(&dbut[S_BSAVE]);
  1333.   }
  1334.  
  1335.   else return(-1);                      /* unhandled character */
  1336.  
  1337.   SetDirFName(filename);
  1338.   return(0);
  1339. }
  1340.  
  1341.  
  1342. /***************************************************/
  1343. void RedrawDNamW()
  1344. {
  1345.   int width, len;
  1346.  
  1347.   len = strlen(filename);
  1348.   XSetForeground(theDisp, theGC, infofg);
  1349.   XDrawString(theDisp, dnamW, theGC, 3, ASCENT+3, filename, len);
  1350.   width = StringWidth(filename);
  1351.   XDrawLine(theDisp, dnamW, theGC, 3+width+1, 3, 3+width+1, 
  1352.         3+CHIGH);
  1353. }
  1354.  
  1355.  
  1356. /***************************************************/
  1357. void DirOpenActive()
  1358. {
  1359.   if (dList.selected>=dList.nstr || dList.selected<0) 
  1360.     BTSetActive(&dbut[S_BOPEN],0);
  1361.  
  1362.   else if (dList.str[dList.selected][0] == C_DIR ||
  1363.       dList.str[dList.selected][0] == C_LNK) 
  1364.     BTSetActive(&dbut[S_BOPEN],1);
  1365.  
  1366.   else
  1367.     BTSetActive(&dbut[S_BOPEN],0);
  1368.  
  1369.   XFlush(theDisp);
  1370. }
  1371.  
  1372.  
  1373.  
  1374. /***************************************************/
  1375. int DoSave()
  1376. {
  1377.   FILE *fp;
  1378.   byte *thepic, *bwpic;
  1379.   int   w,h,rv,i;
  1380.  
  1381.   /* opens file, does appropriate color pre-processing, calls save routine
  1382.      based on chosen format.  Returns '0' if successful */
  1383.  
  1384.   WaitCursor();
  1385.  
  1386.   bwpic = NULL;
  1387.  
  1388.   if (RBWhich(sizeRB)==1) { thepic = epic;  w = eWIDE;  h = eHIGH; }
  1389.                      else { thepic = cpic;  w = cWIDE;  h = cHIGH; }
  1390.  
  1391.   if (RBWhich(colorRB)==2) {
  1392.     /* generate a FSDithered 1-byte per pixel image */
  1393.     bwpic = (byte *) malloc(w*h);
  1394.     if (!bwpic) FatalError("unable to malloc dithered picture (DoSave)");
  1395.     FSDither(thepic, w, h, bwpic);
  1396.     thepic = bwpic;
  1397.   }
  1398.  
  1399.   /* open file */
  1400.   fp = fopen(filename, "w");
  1401.   if (!fp) {
  1402.     SetISTR(ISTR_INFO,"Can't create '%s' -  %s",filename,sys_errlist[errno]);
  1403.     Warning();
  1404.     if (bwpic) free(bwpic);
  1405.     SetCursors(-1);
  1406.     return -1;
  1407.   }
  1408.  
  1409.   if ((mono || ncols==0) && RBWhich(colorRB)==0) {
  1410.     /* if we're saving color, but we're viewing B/W we have to NOT do
  1411.        the 'monofication' of the colormap ... */
  1412.     for (i=0; i<numcols; i++) {
  1413.       r[i] = rorg[i];  g[i] = gorg[i];  b[i] = borg[i];  /* original */
  1414.       
  1415.       if (revvideo) {
  1416.     r[i] = 255-r[i];  g[i] = 255-g[i];  b[i] = 255-b[i];
  1417.       }
  1418.    } 
  1419.  
  1420.     GammifyColors();
  1421.   }
  1422.       
  1423.   rv = 0;
  1424.   i = RBWhich(formatRB);
  1425.   switch (i) {
  1426.   case 0:  rv = WriteGIF(fp,thepic,w, h, r, g, b, numcols, RBWhich(colorRB));
  1427.            break;
  1428.   case 1:  rv = WritePM (fp,thepic,w, h, r, g, b, numcols, RBWhich(colorRB)); 
  1429.            break;
  1430.   case 2:  rv = WritePBM(fp,thepic,w, h, r, g, b, numcols, RBWhich(colorRB),1);
  1431.            break;
  1432.   case 3:  rv = WritePBM(fp,thepic,w, h, r, g, b, numcols, RBWhich(colorRB),0);
  1433.            break;
  1434.   case 4:  rv = WriteXBM(fp,thepic,w, h, filename);
  1435.            break;
  1436.   case 5:  rv = WriteVIS(fp,thepic,w,h,r,g,b,numcols,RBWhich(colorRB));
  1437.       break;
  1438.   }
  1439.  
  1440.   fclose(fp);
  1441.   if (rv) unlink(filename);   /* couldn't properly write file:  delete it */
  1442.  
  1443.   if (!rv) {
  1444.     SetISTR(ISTR_INFO,"Successfully wrote '%s'",filename);
  1445.     LoadCurrentDirectory();   /* wrote file: rescan directory */
  1446.   }
  1447.  
  1448.   if (bwpic) free(bwpic);
  1449.  
  1450.   if ((mono || ncols==0) && RBWhich(colorRB)==0) {
  1451.     /* restore normal colormap */
  1452.     DoMonoAndRV();
  1453.     GammifyColors();
  1454.   }
  1455.  
  1456.   SetCursors(-1);
  1457.  
  1458.   return rv;
  1459. }
  1460.  
  1461.  
  1462.  
  1463. /***************************************************/
  1464. void SetDirFName(st)
  1465. char *st;
  1466. {
  1467.   strncpy(filename, st, MAXFNLEN-1);
  1468.   filename[MAXFNLEN-1] = '\0';  /* make sure it's terminated */
  1469.   XClearWindow(theDisp, dnamW);
  1470.   RedrawDNamW();
  1471.   BTSetActive(&dbut[S_BSAVE], strlen(filename)!=0);
  1472. }
  1473.  
  1474. @//E*O*F xvdir.c//
  1475. chmod u=rw,g=r,o=r xvdir.c
  1476.  
  1477. echo x - xvvis.c
  1478. sed 's/^@//' > "xvvis.c" <<'@//E*O*F xvvis.c//'
  1479. /*
  1480.  * xvvis.c - load routine for 'vis' format pictures
  1481.  *
  1482.  * LoadVIS(fname, numcols)  -  loads a VIS pic, does 24to8 code if nec.
  1483.  * WriteVIS(fp, pic, w, h, r,g,b, numcols, style)
  1484.  */
  1485.  
  1486. /*
  1487.  * Copyright 1989, 1990 by the University of Pennsylvania
  1488.  *
  1489.  * Permission to use, copy, and distribute for non-commercial purposes,
  1490.  * is hereby granted without fee, providing that the above copyright
  1491.  * notice appear in all copies and that both the copyright notice and this
  1492.  * permission notice appear in supporting documentation.
  1493.  *
  1494.  * The software may be modified for your own purposes, but modified versions
  1495.  * may not be distributed.
  1496.  *
  1497.  * This software is provided "as is" without any express or implied warranty.
  1498.  */
  1499.  
  1500.  
  1501. #include "xv.h"
  1502.  
  1503. static int VISError();
  1504.  
  1505. int LoadVIS(fname,nc)
  1506. char *fname;
  1507. int   nc;
  1508. {
  1509.     FILE  *fp;
  1510.     char buf[1000];
  1511.     int w=-1,h=-1;
  1512.     char etype[100];
  1513.     int raster=getenv("VIS_RASTER")&&atoi(getenv("VIS_RASTER"));
  1514.     
  1515.     fp=fopen(fname,"r");
  1516.     if (!fp) return VISError("unable to open file");
  1517.  
  1518.     while(fgets(buf,sizeof buf,fp)) {
  1519.         if(!buf[0]||buf[0]=='\n'||buf[0]=='\f') break;
  1520.         sscanf(buf,"DIMS=%d %d",&w,&h);
  1521.         sscanf(buf,"ETYPE=%s",etype);
  1522.     }
  1523.  
  1524.     if(w<0||h<0) {fclose(fp); return VISError("bad format");}
  1525.  
  1526.     pWIDE=w; pHIGH=h;
  1527.     {int i; for(i=0;i<256;i++) r[i]=g[i]=b[i]=i;}
  1528.  
  1529.     if(!strcmp(etype,"uchar")) {
  1530.         int i,j;
  1531.         pic=(unsigned char*)malloc(w*h);
  1532.         if(!pic) {fclose(fp); return VISError("cannot allocate enough memory");}
  1533.         if(!raster) for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+(h-j-1)*w]=getc(fp);
  1534.         else for(j=0;j<h;j++) for(i=0;i<w;i++) pic[i+j*w]=getc(fp);
  1535.     } else if(!strcmp(etype,"float")) {
  1536.         int i,j,n;
  1537.         float min=1e38,max=-1e38;
  1538.         float *t;
  1539.         t=(float*)malloc(w*h*sizeof *t);
  1540.         if(!t) {fclose(fp); return VISError("cannot allocate enough memory");}
  1541.         pic=(unsigned char*)malloc(w*h); 
  1542.         if(!pic) {free(t); fclose(fp); return VISError("cannot allocate enough memory");}
  1543.         fread(t,sizeof *t,w*h,fp);
  1544.         n=w*h;
  1545.         for(i=0;i<n;i++) {if(t[i]<min) min=t[i]; if(t[i]>max) max=t[i];}
  1546.         if(max==min) max=min+1.0;
  1547.         if(!raster) for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+(h-j-1)*w]=255*(t[i*h+j]-min)/(max-min);
  1548.         else for(i=0;i<w;i++) for(j=0;j<h;j++) pic[i+j*w]=255*(t[i+j*w]-min)/(max-min);
  1549.         free(t);
  1550.     } else {close(fp); return VISError("unknown ETYPE");}
  1551.  
  1552.     fclose(fp);
  1553.     return 0;
  1554. }
  1555.  
  1556. int WriteVIS(fp,thepic,w,h,rmap,gmap,bmap,numcols,colorstyle)
  1557. FILE *fp;
  1558. byte *thepic;
  1559. int w,h;
  1560. byte *rmap,*gmap,*bmap;
  1561. {
  1562.     int i,j;
  1563.  
  1564.     fprintf(fp,"VISF=\nDIMS=%d %d\nETYPE=uchar\n\f\n",w,h);
  1565.  
  1566.     if(colorstyle==2) {
  1567.         for(i=0;i<w;i++) for(j=0;j<h;j++) putc(thepic[i+(h-j-1)*w]?255:0,fp);
  1568.     } else {
  1569.         int rgb[256];
  1570.         for (i=0; i<numcols; i++) rgb[i] = MONO(rmap[i],gmap[i],bmap[i]);
  1571.         for(i=0;i<w;i++) for(j=0;j<h;j++) putc(rgb[thepic[i+(h-j-1)*w]],fp);
  1572.     }
  1573.     return 0;
  1574. }
  1575.  
  1576. static int VISError(st)
  1577. char *st;
  1578. {
  1579.     SetISTR(ISTR_WARNING,"LoadVIS() - %s",cmd,st);
  1580.     Warning();
  1581.     return -1;
  1582. }
  1583.  
  1584. @//E*O*F xvvis.c//
  1585. chmod u=rw,g=r,o=r xvvis.c
  1586.  
  1587. exit 0
  1588.  
  1589.  
  1590.